﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    int sourceStation;
    int destStation;
    int classType;
    DateTime journeyDate;

    string sql, sql2;
    SqlConnection conn;
    SqlCommand cmd, cmd2;
    SqlDataReader reader, reader2;

    DataTable dt,dt2;
    DataColumn col, col2, col3;
    DataRow row;

    protected void Page_Load(object sender, EventArgs e)
    {
        lblJourneyDate.Text = DateTime.Today.ToString("MMM/dd/yyyy");
        cldJD.SelectedDate = DateTime.Today;
    }
    protected void btnJD_Click(object sender, EventArgs e)
    {
        cldJD.Visible = true;
    }
    protected void cldJD_SelectionChanged(object sender, EventArgs e)
    {
        lblJourneyDate.Text = cldJD.SelectedDate.ToString("MMM/dd/yyyy");
        cldJD.Visible = false;
    }
    protected void btnGetDetail_Click(object sender, EventArgs e)
    {
        sourceStation = Convert.ToInt32(ddlSourceStation.SelectedValue);
        destStation = Convert.ToInt32(ddlDestStation.SelectedValue);
        classType = Convert.ToInt32(ddlClass.SelectedValue);
        journeyDate = cldJD.SelectedDate;

        sql = "SELECT RouteID, StationID, PortNo FROM RouteMap WHERE StationID = @id";
        sql2 = "SELECT RouteID, StationID, PortNo FROM RouteMap WHERE StationID = @rid AND StationID = @dsid";
        conn = new SqlConnection("Data Source=DAVID;Initial Catalog=ORRS;User ID=sa;Password=123456");
        try
        {
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", ddlSourceStation.SelectedValue);
            reader = cmd.ExecuteReader();
            
            while (reader.Read())
            {
                Response.Write("ad");
                row = dt.NewRow();
                //row[0] = reader[0];
                //row[1] = reader[1];
                //row[2] = reader[2];
                //dt.Rows.Add(row);
                Response.Write(reader[0] + " _+_" + reader[1] + " _+_" + reader[2] + "<br/>");
            }
            reader.Close();

            int routeID,stationID,portNo,destPort = 0;

            dt = new DataTable();
            col = new DataColumn("RouteID");
            col2 = new DataColumn("StationID");
            col3 = new DataColumn("PortNo");
            dt.Columns.Add(col);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            dt2.Columns.Add(col);
            dt2.Columns.Add(col2);
            dt2.Columns.Add(col3);



/*            foreach (DataRow dr in dt.Rows)
            {
                routeID = Convert.ToInt32(dr[0]);
                stationID = Convert.ToInt32(dr[1]);
                portNo = Convert.ToInt32(dr[2]);

                cmd2 = new SqlCommand(sql2, conn);
                cmd2.Parameters.AddWithValue("@rid", routeID);
                cmd2.Parameters.AddWithValue("@dsid", ddlDestStation.SelectedValue);
                //reader2 = cmd2.ExecuteReader();
                //reader2.Close();
                int rs = cmd2.ExecuteNonQuery();
                if (rs!=0)
                {
                    //Response.Write("asdad ");
                    reader2 = cmd2.ExecuteReader();
                    while (reader2.Read())
                    {
                        destPort = Convert.ToInt32(reader2[3]);
                        if (destPort > portNo)
                        {
                            dt2.Rows.Add(dr);
                            row = dt2.NewRow();
                            row[0] = reader2[0];
                            row[1] = reader2[1];
                            row[2] = reader2[2];
                            dt2.Rows.Add(row);
                        }
                    }
                    reader2.Close();
                }
            }

            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    Response.Write(dr[dc] + " ");
                }
                Response.Write("<br />");
            }
*/        }
        catch (Exception ex)
        {
        }
    }
}
